<?php
require_once 'base.php';
checkAuth();

$op = Utility::getParam('op', 'manage');
switch($op) {
	case 'query':
		$op();
		break;
	case 'doQuery':
		$op();
		break;
	case 'add':
		$op();
		break;
	case 'doAdd':
		$op();
		break;
	case 'modify':
		$op();
		break;
	case 'doModify':
		$op();
		break;
	case 'doDelete':
		$op();
		break;
	case 'manage':
		$op();
		break;
	case 'getCartridgesInfo':
		$op();
		break;
	default:
		echoAjax(Constants::$ERR_MISSING_PARAMS);
		break;
}

function query() {
	
}

function doQuery() {
	
}

function add() {
	
	include $GLOBALS['ROOT_PATH'] .	'/views/admin/machine_add.phtml';
}

function doAdd() {
	
	$name = Utility::getParam('name', '');
	$types = trim(Utility::getParam('types', ''), ',');
	
	if (empty($name) || empty($types)) {
		echoAjax(Constants::$ERR_MISSING_PARAMS);
		return;
	}
	
	$machineInfo = ObjectFinder::get('Machine')->getInfoByName($name);
	if (!empty($machineInfo)) {
		echoAjax(Constants::$ERR_MACHINE_EXIST);
		return ;
	}
	
	$res = ObjectFinder::get('Machine')->add(array('name'=>$name, 'cartridges_type'=>$types));
	
	//同时检查是否存在该硒鼓类型，不存在插入cartridges库，否则新建订单无法看到硒鼓
	$typeArr = explode(",", $types);
	$cartridgeModel = ObjectFinder::get('Cartridge');
	$cartridgeRes = $cartridgeModel->queryByTypes($typeArr);
	if (!empty($cartridgeRes['data'])) {
		$typeArr = array_diff($typeArr, array_keys($cartridgeRes['data']));
	}
	if (!empty($typeArr)) {
		foreach($typeArr as $type) {
			$cartridgeModel->add(array('type' => $type, 'inventory' => json_encode(getDefaultInventory())));
		}
	}
	
	echoAjax($res ? Constants::$ERR_SUCCESS : Constants::$ERR_FAILED);
}

function getDefaultInventory() {
	$warehouses = array_keys($GLOBALS['conf']['warehouse']);
	sort($warehouses);
	$res = array();
	foreach($warehouses as $k) {
		$res[$k]='0';
	}
	return $res;
}

function modify() {
	
	$id = max(0, (int)Utility::getParam('id', 0));
	if (empty($id)) {
		echo Constants::$ERR_INVALID_PARAMS['errmsg'];
		return ;
	}
	
	$machineInfo = ObjectFinder::get('Machine')->getInfoById($id);
	if (empty($machineInfo)) {
		echo Constants::$ERR_QUERY_MACHINE_NOT_EXIST['errmsg'];
		return ;
	}
	$types = explode(',', $machineInfo['cartridges_type']);
	
	include $GLOBALS['ROOT_PATH'] .	'/views/admin/machine_modify.phtml';
}

function doModify() {
	
	$id = max(0, (int)Utility::getParam('id', 0));
	$name = Utility::getParam('name', '');
	$types = trim(Utility::getParam('types', ''), ',');
	
	if (empty($id) || empty($name) || empty($types)) {
		echoAjax(Constants::$ERR_MISSING_PARAMS);
		return;
	}
	
	$machineInfo = ObjectFinder::get('Machine')->getInfoById($id);
	if (empty($machineInfo)) {
		echoAjax(Constants::$ERR_QUERY_MACHINE_NOT_EXIST);
		return ;
	}
	
	ObjectFinder::get('Machine')->modify(array('name'=>$name, 'cartridges_type'=>$types), array('id'=>$id));
	
	//同时检查是否存在该硒鼓类型，不存在插入cartridges库，否则新建订单无法看到硒鼓
	$typeArr = explode(",", $types);
	$cartridgeModel = ObjectFinder::get('Cartridge');
	$cartridgeRes = $cartridgeModel->queryByTypes($typeArr);
	if (!empty($cartridgeRes['data'])) {
		$typeArr = array_diff($typeArr, array_keys($cartridgeRes['data']));
	}
	if (!empty($typeArr)) {
		foreach($typeArr as $type) {
			$cartridgeModel->add(array('type' => $type, 'inventory' => json_encode(getDefaultInventory())));
		}
	}
	
	echoAjax(Constants::$ERR_SUCCESS);
}

function manage() {
	
	$conf = $GLOBALS['conf'];
	$page = (int)Utility::getParam('page', 1);
    $start = (max($page, 1)-1)*$conf['pageSize'];
    
    $machineModel = ObjectFinder::get('Machine');	
	$machineList = $machineModel->getList($start, $conf['pageSize']);
	$machineInfos = array('total'=>0, 'data'=>array());
	if (!empty($machineList['data'])) {
		$machines = array();
		foreach($machineList['data'] as $machine) {
			$machines[] = $machine['name'];
		}
		$machineInfos = $machineModel->getInfoByNames($machines);
	}
	include $GLOBALS['ROOT_PATH'] .	'/views/admin/machine_manage.phtml';
}

function getCartridgesInfo() {
	
	$machineID = max(0, Utility::getParam('mid', 0));
	if (empty($machineID)) {
		echoAjax(Constants::$ERR_MISSING_PARAMS);
		return ;
	}
	
	$machineInfo = ObjectFinder::get('Machine')->getInfoById($machineID);
	if (empty($machineInfo)) {
		echoAjax(Constants::$ERR_QUERY_MACHINE_NOT_EXIST);
		return ;
	}
	
	$types = explode(',', $machineInfo['cartridges_type']);
	foreach($types as $type) {
		$res = ObjectFinder::get('Cartridge')->queryByType($type);
		if (empty($res)) {
			$data[$type] = $res;
		}else {
			$data[$type]['inventory'] = json_decode($res[0]['inventory']);
		}
	}
	echoAjax(array('errno'=>0, 'errmsg'=>'', 'data'=>$data));
}

function doDelete() {
	//echoAjax(array('errno' => 0, 'errmsg' => '灏氭湭瀹炵幇姝ゅ姛鑳�));
	
	$id = max(0, (int)Utility::getParam('id', 0));
	if (empty($id)) {
		echoAjax(Constants::$ERR_MISSING_PARAMS);
		return ;	
	}
	
	$res = ObjectFinder::get('Machine')->delete(array('id'=>$id));
	echoAjax($res ? Constants::$ERR_SUCCESS : Constants::$ERR_FAILED);
}
?>